import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/config/enum/load_state.dart';
import 'package:flutter_changdian_football/pages/home/local_course_detail/local_course_detail_controller.dart';
import 'package:flutter_changdian_football/utils/map_util.dart';
import 'package:flutter_changdian_football/widget/default_page/custom_empty.dart';
import 'package:flutter_changdian_football/widget/load_state/load_state_layout.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:url_launcher/url_launcher.dart';

/// 本地球场详情
class LocalCourseDetailPage extends StatelessWidget {
  const LocalCourseDetailPage({super.key, required this.courtId});

  final String courtId;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
			body: GetBuilder<LocalCourseDetailController>(
        init: LocalCourseDetailController(courtId: courtId),
        builder: (localCourseDetailController) {
          return Stack(
            children: [
              LoadStateLayout(
                state: localCourseDetailController.state,
                success: SingleChildScrollView(
                  controller: localCourseDetailController.scrollController,
                  child: Stack(
                    children: [
                      Stack(
                        alignment: Alignment.bottomCenter,
                        children: [
                          CarouselSlider.builder(
                            itemCount: localCourseDetailController.courtDetailModel?.courtImages?.length, 
                            itemBuilder: (context, index, realIndex) {
                              return SizedBox(
                                width: double.infinity,
                                child: Image.network(localCourseDetailController.courtDetailModel?.courtImages?[index].courtImageUrl ?? '',
                                  excludeFromSemantics: true, gaplessPlayback: true, fit: BoxFit.fill,),
                              );
                            }, 
                            options: CarouselOptions(
                              enlargeCenterPage: false, 
                              enableInfiniteScroll : false,
                              animateToClosest : false,
                              height: 285.w,
                              aspectRatio: 0,
                              viewportFraction: 1.0,
                              onPageChanged: (index, reason) {
                                localCourseDetailController.changeBannerIndex(index);
                              },
                            )
                          ),

                          Positioned(
                            right: 17.w,
                            bottom: 37.w,
                            child: SizedBox(
                              child: Text(
                                '${localCourseDetailController.currentBannerIndex + 1}/${localCourseDetailController.courtDetailModel?.courtImages?.length}',
                                style: TextStyle(fontSize: 14.sp, color: Colors.white),
                              ),
                            )
                          )
                        ],
                      ),

                      Container(
                        margin: EdgeInsets.only(top: 258.w ),
                        width: double.infinity,
                        // height: MediaQuery.of(context).size.height - kToolbarHeight - MediaQuery.of(context).padding.top + 17.w,
                        // height: 140.w + (30 * 51.w),
                        height: (localCourseDetailController.courtDetailModel?.activityClubVOS?.length ?? 0) < 15 ? 
                          MediaQuery.of(context).size.height - kToolbarHeight - MediaQuery.of(context).padding.top + 17.w : 
                          160.w + ((
                            (((localCourseDetailController.courtDetailModel?.activityClubVOS?.length ?? 0) + 1) % 3) == 0 ?
                            (localCourseDetailController.courtDetailModel?.activityClubVOS?.length ?? 0) + 1 :
                            (((localCourseDetailController.courtDetailModel?.activityClubVOS?.length ?? 0) + 1) % 3) == 1 ?
                            (localCourseDetailController.courtDetailModel?.activityClubVOS?.length ?? 0) + 3 :
                            (localCourseDetailController.courtDetailModel?.activityClubVOS?.length ?? 0) + 2
                          ) / 3 * 140.w), 
                        decoration: BoxDecoration(
                          color: const Color(0xffF8FAF9),
                          borderRadius: BorderRadius.only(
                            topLeft: Radius.circular(22.r),
                            topRight: Radius.circular(22.r),
                          )
                        ),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            SizedBox(height: 17.w,),

                            Container(
                              padding: EdgeInsets.symmetric(horizontal: 17.w),
                              child: Text(localCourseDetailController.courtDetailModel?.courtName ?? '', style: TextStyle(fontSize: 18.sp, fontWeight: FontWeight.bold, color: const Color(0xff323232)),),
                            ),

                            SizedBox(height: 9.w,),

                            GestureDetector(
                              behavior: HitTestBehavior.translucent,
                              onTap: () {
                                MapUtil.openMap(localCourseDetailController.courtDetailModel?.lng, localCourseDetailController.courtDetailModel?.lat, address: localCourseDetailController.courtDetailModel?.courtName);
                              },
                              child: Container(
                                padding: EdgeInsets.symmetric(vertical: 7.w),
                                margin: EdgeInsets.symmetric(horizontal: 18.w, ),
                                decoration: BoxDecoration(
                                  border: Border(
                                    top: BorderSide(
                                      width: 0.5.w,
                                      color: const Color(0xffF0F0F0)
                                    ),
                                    bottom: BorderSide(
                                      width: 0.5.w,
                                      color: const Color(0xffF0F0F0)
                                    )
                                  )
                                ),
                                child: Row(
                                  children: [
                                    SizedBox(
                                      height: 18.w,
                                      width: 16.w,
                                      child: Image.asset(
                                        'assets/images/img_list_dw02.png'
                                      ),
                                    ),
                            
                                    SizedBox(width: 10.w,),
                            
                                    Expanded(
                                      child: Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: [
                                          SizedBox(
                                            child: Text(localCourseDetailController.courtDetailModel?.courtAddress ?? '', style: TextStyle(fontSize: 12.sp, color: const Color(0xff333333)),),
                                          ),
                                          SizedBox(
                                            child: Text(localCourseDetailController.courtDetailModel?.distance ?? '', style: TextStyle(fontSize: 11.sp, color: const Color(0xff999999)),),
                                          ),
                                        ],
                                      ),
                                    ),
                            
                                    if (localCourseDetailController.courtDetailModel?.courtTelephone != null) ...[
                                      GestureDetector(
                                        behavior: HitTestBehavior.translucent,
                                        onTap: () {
                                          final Uri smsLaunchUri = Uri(
                                            scheme: 'tel',
                                            path: localCourseDetailController.courtDetailModel?.courtTelephone,
                                          );
                                          launchUrl(smsLaunchUri);
                                        },
                                        child: SizedBox(
                                          width: 20.w,
                                          height: 20.w,
                                          child: Image.asset(
                                            'assets/images/icon_list_ph.png'
                                          ),
                                        ),
                                      )
                                    ]
                                  ],
                                ),
                              ),
                            ),

                            SizedBox(height: 21.w,),

                            Container(
                              padding: EdgeInsets.symmetric(horizontal: 18.w),
                              child: Row(
                                children: [
                                  Container(
                                    width: 3.w,
                                    height: 14.w,
                                    margin: EdgeInsets.symmetric(vertical: 5.w),
                                    color: const Color(0xff499360),
                                  ),
                                  SizedBox(width: 8.w,),
                                  SizedBox(
                                    child: Text('主场俱乐部', style: TextStyle(fontSize: 16.sp, fontWeight: FontWeight.bold, color: const Color(0xff323232)),),
                                  )
                                ],
                              ),
                            ),

                            SizedBox(height: 17.w,),

                            
                            if ((localCourseDetailController.courtDetailModel?.activityClubVOS?.length ?? 0) <= 0) ...[
                              const SizedBox(
                                child: CustomEmpty(
                                  text: '该主场暂无俱乐部',
                                  img: 'assets/images/img_empty_club.png',
                                ),
                              )
                            ] else ...[
                              SizedBox(
                                child: GridView.builder(
                                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                                    crossAxisCount: 3,
                                    childAspectRatio: 0.90
                                  ),
                                  itemCount: localCourseDetailController.courtDetailModel?.activityClubVOS?.length,
                                  shrinkWrap: true,
                                  padding: EdgeInsets.zero,
                                  physics: const NeverScrollableScrollPhysics(),
                                  itemBuilder: (BuildContext context, int index) {
                                    return SizedBox(
                                      child: Column(
                                        children: [
                                          Container(
                                            width: 88.w,
                                            height: 88.w,
                                            decoration: BoxDecoration(
                                              borderRadius: BorderRadius.circular(10.r),
                                            ),
                                            child: ClipRRect(
                                              borderRadius: BorderRadius.circular(10.r),
                                              child: Image.network(localCourseDetailController.courtDetailModel?.activityClubVOS?[index].clubImage ?? '', gaplessPlayback: true, excludeFromSemantics: true, fit: BoxFit.fill,),
                                            ),
                                          ),

                                          SizedBox(height: 10.w,),

                                          SizedBox(
                                            child: Text(localCourseDetailController.courtDetailModel?.activityClubVOS?[index].clubName ?? '', style: TextStyle(fontSize: 14.sp, color: const Color(0xff323232), fontWeight: FontWeight.w500),),
                                          )
                                        ],
                                      ),
                                    );
                                  },
                                ),
                              )
                            ]
                          ],
                        ),
                      )
                    ],
                  ),
                ),
              ),

              Positioned(
                top: 0,
                left: 0,
                right: 0,
                child: PreferredSize(
                  preferredSize: const Size.fromHeight(kToolbarHeight), // 设置 AppBar 的高度为 0
                  child: AppBar(
                    elevation: 0, 
                    backgroundColor: const Color(0xffF8FAF9).withAlpha(localCourseDetailController.titleAlpha),
                    iconTheme: IconThemeData(
                      color: localCourseDetailController.state == LoadState.success ? (localCourseDetailController.titleAlpha <= 200 ? Colors.white : const Color(0xff333333)) : const Color(0xff333333)
                    ),
                    title: localCourseDetailController.state == LoadState.success ? Container() : Text(localCourseDetailController.courtDetailModel?.courtName ?? '', style: TextStyle(fontSize: 16.sp, color: const Color(0xff333333).withAlpha(localCourseDetailController.titleAlpha)),),
                    centerTitle: true,
                  ),
                ),
              )
            ]
          );
        }
      )
		);
  }
}